package com.chuanye.jiguangimdemo.activitys;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.chuanye.jiguangimdemo.R;
import com.chuanye.jiguangimdemo.adapters.ForwardMsgAdapter;

import java.util.ArrayList;
import java.util.List;

import cn.jpush.im.android.api.JMessageClient;
import cn.jpush.im.android.api.content.TextContent;
import cn.jpush.im.android.api.enums.ConversationType;
import cn.jpush.im.android.api.model.Conversation;
import cn.jpush.im.android.api.model.Message;
import cn.jpush.im.android.api.options.MessageSendingOptions;
import cn.jpush.im.api.BasicCallback;

public class ForwardMsgActivity extends BaseActivity implements View.OnClickListener {
    private static final String TAG = "ForwardMsgActivity";
    private Context mContext;
    private ImageView forwardmsg_back_iv;
    private LinearLayout forwardmsg_search_title_ll;
    private LinearLayout forwardmsg_groupAll_ll;
    private ListView forwardmsg_business_list_lv;
    private TextView forwardmsg_letter_hint_tv;
    private TextView forwardmsg_title_tv;

    private ForwardMsgAdapter mAdapter;
    private List<Conversation> forwardList = new ArrayList<>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forward_msg);
        mContext = this;

        initView();

        initData();
    }

    private void initView() {
        //返回
        forwardmsg_back_iv = findViewById(R.id.forwardmsg_back_iv);
        forwardmsg_back_iv.setOnClickListener(this);
        //标题
        forwardmsg_title_tv = findViewById(R.id.forwardmsg_title_tv);
        //搜索
        forwardmsg_search_title_ll = findViewById(R.id.forwardmsg_search_title_ll);
        forwardmsg_search_title_ll.setOnClickListener(this);
        forwardmsg_groupAll_ll = findViewById(R.id.forwardmsg_groupAll_ll);
        forwardmsg_groupAll_ll.setOnClickListener(this);
        //listview
        forwardmsg_business_list_lv = findViewById(R.id.forwardmsg_business_list_lv);
        forwardmsg_letter_hint_tv = findViewById(R.id.forwardmsg_letter_hint_tv);

        int flags = getIntent().getFlags();
        if (flags == 1) {
            forwardmsg_title_tv.setText( "发送名片");
        } else {
            forwardmsg_title_tv.setText( "转发");
        }
    }

    private void initData() {
        List<Conversation> conversationList = JMessageClient.getConversationList();
        for (Conversation conv : conversationList) {
            if (!conv.getTargetId().equals("feedback_Android") && conv.getType() != ConversationType.chatroom) {
                forwardList.add(conv);
            }
        }
        mAdapter = new ForwardMsgAdapter(mContext, forwardList);
        forwardmsg_business_list_lv.setAdapter(mAdapter);


        //最近联系人
        forwardmsg_business_list_lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @SuppressLint("WrongConstant")
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                final Object itemAtPosition = parent.getItemAtPosition(position);
                //发送名片
                final Intent intent = getIntent();
                final Conversation conversation = (Conversation) itemAtPosition;
                if (intent.getFlags() == 1) {
                    String toName = conversation.getTitle();
                    setBusinessCard(toName, intent, conversation);
                    //转发消息
                } else {
                    //DialogCreator.createForwardMsg(ForwardMsgActivity.this, mWidth, true, conversation, null, null, null);
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.forwardmsg_back_iv://返回
                finish();
                break;
            case R.id.forwardmsg_search_title_ll://搜索
                Intent intent = new Intent(ForwardMsgActivity.this, SearchContactsActivity.class);
                setExtraIntent(intent);
                break;
            case R.id.forwardmsg_groupAll_ll://群组
                Intent intent1 = new Intent(ForwardMsgActivity.this, GroupActivity.class);
                setExtraIntent(intent1);
                break;
        }
    }

    @SuppressLint("WrongConstant")
    private void setExtraIntent(Intent intent) {
        //发送名片,ForwardMsgActivity跳转过来
        if (getIntent().getFlags() == 1) {
            intent.setFlags(2);
            intent.putExtra("userName", getIntent().getStringExtra("userName"));
            intent.putExtra("appKey", getIntent().getStringExtra("appKey"));
            intent.putExtra("avatar", getIntent().getStringExtra("avatar"));
        } else {
            //转发消息,启动群组界面,设置flag标识为1
            intent.setFlags(1);
        }
        startActivity(intent);
    }

    private void setBusinessCard(String name, final Intent intent, final Conversation conversation) {
        //把名片的userName和appKey通过extra发送给对方
        TextContent content = new TextContent("推荐了一张名片");
        content.setStringExtra("userName", intent.getStringExtra("userName"));
        content.setStringExtra("appKey", intent.getStringExtra("appKey"));
        content.setStringExtra("businessCard", "businessCard");

        Message textMessage = conversation.createSendMessage(content);
        MessageSendingOptions options = new MessageSendingOptions();
        options.setNeedReadReceipt(true);
        JMessageClient.sendMessage(textMessage, options);
        textMessage.setOnSendCompleteCallback(new BasicCallback() {
            @Override
            public void gotResult(int i, String s) {
                if (i == 0) {
                    Toast.makeText(ForwardMsgActivity.this, "发送成功", Toast.LENGTH_SHORT).show();
                } else {
                    //HandleResponseCode.onHandle(ForwardMsgActivity.this, i, false);
                }
            }
        });
    }


}
